<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 通知管理 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除 </el-button>
                <el-button type="primary" icon="el-icon-plus" class="handle-del mr10" @click="addNotice"> 添加通知 </el-button>
                <el-select v-model="query.noticeType" placeholder="类型" class="handle-select mr10">
                    <el-option key="2" label="全部" value=""></el-option>
                    <el-option key="0" label="通知" value="0"></el-option>
                    <el-option key="1" label="公告" value="1"></el-option>
                </el-select>
                <el-input v-model="query.noticeTitle" placeholder="标题" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                :data="tableData"
                v-loading="loading"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="noticeId" label="通知编号" align="center"></el-table-column>
                <el-table-column prop="noticeTitle" label="标题" align="center"></el-table-column>
                <el-table-column label="类型" align="center">
                    <template slot-scope="scope">
                        <el-tag type="success" v-show="scope.row.noticeType == 0">通知</el-tag>
                        <el-tag v-show="scope.row.noticeType == 1">公告</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="createTime" label="创建时间" align="center"></el-table-column>
                <el-table-column width="300px" prop="summary" label="简介" align="center"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-tickets" class="blue" @click="queryNotice(scope.row)">详情 </el-button>

                        <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)"
                            >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="jumper,total, prev, pager, next, sizes"
                    :page-sizes="[5, 10, 20, 30]"
                    @size-change="handleSizeChange"
                    :current-page="query.pageNo"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <!-- 编辑弹出框 -->
        <el-dialog :title="title" :visible.sync="open" width="80%">
            <div v-html="content"></div>
        </el-dialog>
    </div>
</template>

<script>
import { noticeData, deleteNotice, batchDelete, noticeById } from '../../api/notice';

export default {
    name: 'notice',
    data() {
        return {
            query: {
                noticeType: '',
                noticeTitle: '',
                pageNo: 1,
                pageSize: 5
            },
            tableData: [],
            loading:true,
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            idx: -1,
            id: -1,
            title: '',
            open: false,
            content: ''
        };
    },
    created() {
        this.getData();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
            this.loading=true;
            noticeData(this.query).then((res) => {
                this.tableData = res.data.data;
                this.pageTotal = res.data.count;
                this.loading=false;
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageNo', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(() => {
                deleteNotice(row.noticeId).then((res) => {
                    this.getData();
                    this.$message.success('删除成功');
                });
            });
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].noticeId + ',';
            }
            this.$confirm('确定要删除编号为' + str + '的数据吗？', '提示', {
                type: 'warning'
            }).then(() => {
                batchDelete(str).then((res) => {
                    this.getData();
                    this.$message.success('删除成功');
                });
            });
            this.multipleSelection = [];
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageNo', val);
            this.getData();
        },
        handleSizeChange(val) {
            this.query.pageSize = val;
            this.getData();
        },
        addNotice() {
            this.$router.push('/addNotice');
        },
        queryNotice(row) {
            noticeById(row.noticeId).then((res) => {
                this.content = res.data.content;
                if (this.content == null || this.content == '') {
                    this.$message.success('该通知无内容');
                } else {
                    this.open = true;
                }
            });
        }
    }
};
</script>

<style scoped>
body {
    margin: 0;
}
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}

.table {
    width: 100%;
    font-size: 14px;
}

.red {
    color: #ff0000;
}

.mr10 {
    margin-right: 10px;
}

.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
